package net.yegong.matrix;

/**
 * @author cooper
 */
public class ColumnVector extends Matrix {

	public ColumnVector(float[] array) {
		super(array, array.length, 1, Matrix.FORTRAN);
	}

	public ColumnVector(int length) {
		super(length, 1);
	}

	@Override
	public float[] getRowMajorArrayCopy() {
		return MatrixHelper.copy(array);
	}

	@Override
	public RowVector transpose() {
		return new RowVector(array);
	}

	@Override
	public void transposeMe() {
	}

	@Override
	public float norm() {
		return (float) Math.sqrt(sumSquare());
	}

	public float get(int i) {
		return array[i];
	}

	public void set(int i, float value) {
		array[i] = value;
	}

	@Override
	public ColumnVector rightMultiply(Matrix m) {
		if (m instanceof RowVector)
			return rightMultiply((RowVector) m);
		if (array.length != m.nCol)
			throw new MatrixDimensionException();
		float[] array2 = new float[m.nRow];
		for (int i = 0; i < m.nRow; ++i) {
			for (int j = 0; j < array.length; ++j) {
				array2[i] += array[j] * m.array[i + j * m.nRow];
			}
		}
		return new ColumnVector(array2);
	}

	public Matrix leftMultiply(RowVector v) {
		int nRow = array.length;
		int nCol = v.array.length;
		float[] array2 = new float[nRow * nCol];
		for (int i = 0; i < nRow; ++i) {
			for (int j = 0; j < nCol; ++j) {
				array2[i + j * nRow] = array[i] * v.array[j];
			}
		}
		return new Matrix(array2, nRow, nCol, Matrix.FORTRAN);
	}

	public ColumnVector rightMultiply(RowVector v) {
		if (array.length != v.array.length)
			throw new MatrixDimensionException();
		float[] value = { 0.0f };
		for (int i = 0; i < array.length; ++i) {
			value[0] += array[i] * v.array[i];
		}
		return new ColumnVector(value);
	}

	public float innerProduct(RowVector v) {
		if (array.length != v.array.length)
			throw new MatrixDimensionException();
		float value = 0.0f;
		for (int i = 0; i < array.length; ++i) {
			value += array[i] * v.array[i];
		}
		return value;
	}

	@Override
	public ColumnVector multiply(float alpha) {
		ColumnVector ret = clone();
		int len = array.length;
		for (int i = 0; i < len; ++i) {
			ret.array[i] *= alpha;
		}
		return ret;
	}

	@Override
	public ColumnVector clone() {
		return new ColumnVector(MatrixHelper.copy(array));
	}
}
